/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BuildTrainSet.AssignTag;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author Pham Thanh
 */
public class NumTag_SplitCom {
//    public static void main(String argv[]) throws UnsupportedEncodingException, FileNotFoundException, XMLStreamException, IOException{
//        NumTag_SplitCom n=new NumTag_SplitCom("trainset//hand//num_hand.xml", "trainset//hand//num_ncom_split.xml");
//    }
    public  NumTag_SplitCom(String inf, String ouf) throws UnsupportedEncodingException, FileNotFoundException, XMLStreamException, IOException{
        FileInputStream fis = new FileInputStream(inf);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);
        int com=0;

        System.out.println("package BuildTrainSet.AssignTag; class Numtag_SplitCom");

        FileOutputStream fos = new FileOutputStream(ouf);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);

                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }

                    if (localName.equals("W")) {
                        String attributeName = xmlStreamReader.getAttributeLocalName(0);
                        String attributeValue = xmlStreamReader.getAttributeValue(0);
                        if (!attributeValue.equals("NCOM")) {
                            String nsw = xmlStreamReader.getElementText();
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute(attributeName, attributeValue);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();

                        } else {
                            String nsw = xmlStreamReader.getElementText();
                            ArrayList ar=new ArrayList();
                            splitNCom(nsw, ar);
//                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeStartElement("NCOM");
                                for (int i = 0; i < ar.size(); i++) {
                                    xmlStreamWriter.writeStartElement("W");
//                                    xmlStreamWriter.writeAttribute("IN", "NCOM");
                                    if(isNum((String)ar.get(i))){
                                        xmlStreamWriter.writeAttribute("NSW", "NUMBER");
                                     
                                    }else{
                                        xmlStreamWriter.writeAttribute("NSW", "OTHER");
                                    }
                                    
                                    xmlStreamWriter.writeCharacters((String) ar.get(i));
                                    xmlStreamWriter.writeEndElement();
                                    com++;

                                }
                            com--;

                            xmlStreamWriter.writeEndElement();
                        }
                    }

                    if (localName.equals("SPLIT")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();

                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    if (endLocalName.equals("SPLIT")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }

        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();

        System.out.println("    +So diem phan tach: "+com);
        fis.close();
        fos.close();

    }

    public void splitNCom(String nsw, ArrayList ar) {
//        Pattern SPLIT = Pattern.compile("((\\d[\\p{P}\\+&&[^/:.]])|([\\p{P}\\+&&[^/:.]]\\d))");
          Pattern SPLIT = Pattern.compile("((\\d\\p{P})|(\\p{P}\\d))");
        Pattern SPLITNGANG = Pattern.compile("((\\d\\-)|(\\-\\d))");
        Pattern SPLITCHEO = Pattern.compile("((\\d/)|(/\\d))");
        Matcher m=null;
        if(isSplitNgang(nsw)){
            splitNgang(nsw, ar);
        }else{
            if(isSplitHaiCham(nsw)){
                splitHaiCham(nsw, ar);
            }else{
                if(isSplitCheo(nsw)){
                    splitCheo(nsw, ar);
                }else{
                split(nsw, ar);
                }
            }
        }
        
       
    }
      public void splitNgang(String nsw, ArrayList ar) {

        Pattern SPLITNGANG = Pattern.compile("((\\d\\-)|(\\-\\d))");
       
    
        Matcher    m=SPLITNGANG.matcher(nsw);
      
        
        int lastIndex = 0;
        if (m.find()) {
            String sNsw = nsw.substring(lastIndex, m.start() + 1);
            ar.add(sNsw);
            nsw = nsw.substring(m.end() - 1);
            splitNgang(nsw, ar);
        } else {
            ar.add(nsw);
        }
    }
      public void splitCheo(String nsw, ArrayList ar) {

        Pattern SPLITCHEO = Pattern.compile("((\\d/)|(/\\d))");
       
    
        Matcher    m=SPLITCHEO.matcher(nsw);
      
        
        int lastIndex = 0;
        if (m.find()) {
            String sNsw = nsw.substring(lastIndex, m.start() + 1);
            ar.add(sNsw);
            nsw = nsw.substring(m.end() - 1);
            splitCheo(nsw, ar);
        } else {
            ar.add(nsw);
        }
    }
       public void splitHaiCham(String nsw, ArrayList ar) {

        Pattern SPLITCHEO = Pattern.compile("((\\d[\\p{P}&&[^/]])|([\\p{P}&&[^/]]\\d))");
       
    
        Matcher    m=SPLITCHEO.matcher(nsw);
      
        
        int lastIndex = 0;
        if (m.find()) {
            String sNsw = nsw.substring(lastIndex, m.start() + 1);
            ar.add(sNsw);
            nsw = nsw.substring(m.end() - 1);
            splitHaiCham(nsw, ar);
        } else {
            ar.add(nsw);
        }
    }
      public void split(String nsw, ArrayList ar) {

          Pattern SPLIT = Pattern.compile("((\\d\\p{P})|(\\p{P}\\d))");
       
    
        Matcher    m=SPLIT.matcher(nsw);
      
        
        int lastIndex = 0;
        if (m.find()) {
            String sNsw = nsw.substring(lastIndex, m.start() + 1);
            ar.add(sNsw);
            nsw = nsw.substring(m.end() - 1);
            splitCheo(nsw, ar);
        } else {
            ar.add(nsw);
        }
    }
public boolean isSplitNgang(String nsw){
    Pattern Ngang=Pattern.compile("\\p{Pd}");
    Matcher m=Ngang.matcher(nsw);
    return m.find();
}

public boolean isSplitCheo(String nsw){
    if(nsw.contains("/"))return true;
    return false;
}
public boolean isSplitHaiCham(String nsw){
    if(nsw.contains(":"))return true;
    return false;
}
    public boolean isNum(String nsw){
        for(int i=0;i<nsw.length();i++){
            if(Character.isDigit(nsw.charAt(i))){
                return true;
            }
        }
        return false;
    }

}
